package dev.alm.javaproject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import dev.alm.javaproject.entity.User;
import dev.alm.javaproject.dto.UserAndDept;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    // 查询所有用户
    @Select("select * from tb_user")
    public List<User> getAllUserList();

    // 添加一个用户
    // 这里values后面仍然传入了id占位，虽然id是由数据库自增的，这样我们不需要在values前面指定字段了
    @Insert("insert into tb_user values (#{id}, #{username}, #{password})")
    public int addUser(User user);

    // 根据id查询用户
    @Select("select * from tb_user where id=#{id}")
    public List<User> getUserById(Long id);

    // 根据id删除一个用户
    @Delete("delete from tb_user where id=#{id}")
    public int deleteUserById(Long id);

    // 更新数据
    @Update("update tb_user set username=#{username}, password=#{password} where id=#{id}")
    public int updateUser(User user);

    // 查询用户及其部门
    @Select("select u.*, d.name as deptName, d.address as deptAddress from tb_user u left join tb_department d on u.dept_id=d.id")
    public List<UserAndDept> getUserAndDepartment();

    // 以下都是通过xml查询
    public List<User> getUserList();

}
